/*
 * @FilePath: /tads/server/app/business/router/router.go
 * @Author: wnina0x0
 * @Date: 2025-04-09 10:55:17
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
package router

import (
	"tads/server/common/auth"
	"time"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
)

var (
	routerNoCheckRole = make([]func(*gin.RouterGroup), 0)
	routerCheckRole   = make([]func(v1 *gin.RouterGroup), 0)
)

// initRouter 路由示例
func InitRouter(port string) {
	route := gin.Default()
	route.SetTrustedProxies([]string{"*"})
	route.Use(cors.New(cors.Config{
		AllowAllOrigins: true,
		AllowMethods:    []string{"GET", "POST", "PUT", "DELETE", "PATCH"},
		AllowHeaders:    []string{"Content-Type", "Access-Token", "Authorization"},
		MaxAge:          6 * time.Hour,
	}))
	// 无需认证的路由
	// noCheckRoleRouter(r)
	// 需要认证的路由
	checkRoleRouter(route)
	noCheckRoleRouter(route)
	route.Run(":" + port)

}

// noCheckRoleRouter 无需认证的路由示例
func noCheckRoleRouter(r *gin.Engine) {
	// 可根据业务需求来设置接口版本
	v1 := r.Group("/api")

	for _, f := range routerNoCheckRole {
		f(v1)
	}
}

// checkRoleRouter 需要认证的路由示例
func checkRoleRouter(r *gin.Engine) {
	// 可根据业务需求来设置接口版本
	v1 := r.Group("/api")
	v1.Use(auth.AuthMiddleware())
	for _, f := range routerCheckRole {
		f(v1)
	}
}
